package com.zwh.tool.utils;

/**
 * 只是用来存放一些学习时会常见到的数字，进制换算学习用，没有实际使用价值
 *
 * @author yexi
 */
public enum NumUtils {

    //比特
    BIT {
        @Override
        public long toBit(long num) {
            return num;
        }

        @Override
        public long toByte(long num) {
            return num / EIGHT;
        }

        @Override
        public long toKB(long num) {
            return num / (EIGHT * ONE_THOUSAND_AND_TWENTY_FOUR);
        }

        @Override
        public long toMB(long num) {
            return num / (EIGHT * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR);
        }

        @Override
        public long toGB(long num) {
            return num / (EIGHT * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR);
        }

        @Override
        public long convert(long d, NumUtils u) {
            return u.toBit(d);
        }
    },
    BYTE {
        @Override
        public long toBit(long num) {
            return num * EIGHT;
        }

        @Override
        public long toByte(long num) {
            return num;
        }

        @Override
        public long toKB(long num) {
            return num / ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toMB(long num) {
            return num / (ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR);
        }

        @Override
        public long toGB(long num) {
            return num / (ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR);
        }

        @Override
        public long convert(long d, NumUtils u) {
            return u.toByte(d);
        }
    },
    //千字节(KB)
    KB {
        @Override
        public long toBit(long num) {
            return num * EIGHT * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toByte(long num) {
            return num * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toKB(long num) {
            return num;
        }

        @Override
        public long toMB(long num) {
            return num / ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toGB(long num) {
            return num / (ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR);
        }

        @Override
        public long convert(long d, NumUtils u) {
            return u.toKB(d);
        }
    },
    //千兆字节(GB)
    MB {
        @Override
        public long toBit(long num) {
            return num * EIGHT * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toByte(long num) {
            return num * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toKB(long num) {
            return num * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toMB(long num) {
            return num;
        }

        @Override
        public long toGB(long num) {
            return num / ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long convert(long d, NumUtils u) {
            return u.toMB(d);
        }
    },
    GB {
        @Override
        public long toBit(long num) {
            return num * EIGHT * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toByte(long num) {
            return num * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toKB(long num) {
            return num * ONE_THOUSAND_AND_TWENTY_FOUR * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toMB(long num) {
            return num * ONE_THOUSAND_AND_TWENTY_FOUR;
        }

        @Override
        public long toGB(long num) {
            return num;
        }

        @Override
        public long convert(long d, NumUtils u) {
            return u.toGB(d);
        }
    };

    /**
     * 1比特
     */
    public static final long BIT_NUM = 1;

    /**
     * 2,二进制基数
     */
    private static final long BASE = 0B10;
    /**
     * 8,2的3次方
     */
    private static final long EIGHT = BASE << 3 - 1;
    /**
     * 1024,2的10次方
     */
    private static final long ONE_THOUSAND_AND_TWENTY_FOUR = BASE << 10 - 1;


    public long toBit(long num) {
        return -1;
    }

    public long toByte(long num) {
        return -1;
    }

    public long toKB(long num) {
        return -1;
    }

    public long toMB(long num) {
        return -1;
    }

    public long toGB(long num) {
        return -1;
    }

    public long convert(long sourceNum, NumUtils utils) {
        return -1;
    }

    //抄过来的HashMap底层的必然保证将一个数字转换成2的几次幂
    public static int changeToSeveralPowersOfTwo(int num) {
        int n = num - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : n + 1;
    }

    /**
     * 十进制转其他进制
     *
     * @param number 十进制数字
     * @param radix  要转换的进制
     * @return
     */
    public static String decimalToScale(long number, int radix) {
        if (2 > radix || radix > 32) {
            throw new IllegalArgumentException("该进制不可转换");
        }
        StringBuilder result = new StringBuilder();
        while (0 != number) {
            result.insert(0, number % radix);
            number = number / radix;
        }
        return result.toString();
    }

}
